package com.helloandroid.services

import com.helloandroid.tools.MyLog
import dai.common.util.CommonUtils

class LocalUserInfo(
        var serverTimeMs:Long = 0L,
        var userId: Int = -1, // 用户id
        var sysBs: Int = 0, // 系统步数
        var bsTarget: Int = 6000,  // 目标步数
        var bs: Int = 0,  // 当前步数
        var lastSysBs: Int = 0, // 上次系统步数

) {
    // 签到
    val signInfo = LocalSignInfo()
    val cfInfo = LocalChiFanInfo()
    val bsInfo = LocalBuShuInfo()
    val hbyInfo = LocalHbyInfo()
    val residentInfo = LocalResidentInfo()

    fun loadInfo(newUserId: Int) {

        MyLog.info("=============LocalUserInfo::loadInfo called!===========================")

        if (userId > 0 && userId != newUserId) {
            // 玩家退出了微信绑定
            // 本地数据需要清空还是重新设置?
            // 清除本地数据
            signInfo.clearData()
            cfInfo.clearData()
            bsInfo.clearData()
            hbyInfo.clearData()
            residentInfo.clearData()
        }
        userId = newUserId

        // TODO: 1.统一先判断是否提醒
        //  后面可以优化一下
        //
        // var curDayTime = MyTimeUtils.dayTimeString(serverTimeMs)
        //--------------
        // init 常驻通知
        residentInfo.init()

        //----------------------------------------------------
        // init 签到信息
        signInfo.init()

        //-------------------------------------------------
        // init 吃饭信息
        cfInfo.init()

        //----------------------------------------------------
        // init 步数信息
        bsInfo.init()

        //--------------------------------------------------
        // init 红包雨信息
        hbyInfo.init()
    }

    // 返回 [0.0,1.0]
    fun calPercent(): Float {
        return CommonUtils.clamp01(bs / bsTarget.toFloat())
    }

    fun checkSignRemind(service: ServerService): Boolean {
        return signInfo.check(service,serverTimeMs)
    }

    fun checkChiFanRemind(service: ServerService): Boolean {
        return cfInfo.check(service,serverTimeMs)
    }

    fun checkBuShuRemind(service: ServerService):Boolean{
        return bsInfo.check(service,serverTimeMs)
    }

    fun checkHbyRemind(service: ServerService):Boolean{
        return hbyInfo.check(service,serverTimeMs)
    }

    // 更新签到时间
    fun updateSignTime(){
        signInfo.saveSignTime(serverTimeMs)
    }

    // 更新步数打卡时间
    fun updateBuShuDaKaSuccessTime(){
        bsInfo.saveDakaTime(serverTimeMs)
    }

    // 更新玩红包雨的时间
    fun updatePlayHbyTime(){
        hbyInfo.savePlayHbyTime(serverTimeMs)
    }

    //
    fun updateChiFan(cfTime: ChiFanTime){
        cfInfo.saveChifan(cfTime,serverTimeMs)
    }

    //====================================
    fun getResidientFlag(){

    }
}